
#include "StateShop.h"
#include "StateShopAnimals.h"
#include "StateShopPowerUps.h"
#include "StatePopUp.h"
#include "StateCoinsPopUp.h"

#include "StateMainMenu.h"
#include "StateManager.h"
#include "InputManager.h"
#include "AudioUtils.h"
#include "ProgressManager.h"

#define ITEMS_PER_ROW        3

void StateShopPowerUps::loadContent()
{
    StateShopCommon::loadContent();
    
    m_oTitleText->setText("PowerUps");
    
    for(int i=0; i<POWERUPS_ITEMS_COUNT; ++i)
    {
        Vector2 vPos = Vector2(120 + 200 * (i % ITEMS_PER_ROW), 720 - 160 * (int)(i / ITEMS_PER_ROW));
        
        PowerupData data = ProgressManager::instance().getPowerupData(i);
        
        m_oIcon[i] = new RenderableEntity(data.sIcon, vPos);
        m_oIcon[i]->setCentered();
        m_oIcon[i]->setScale(Vector2(0.9f, 0.9f));
        
        m_oName[i] = new TextEntity(data.sCaption, FONT_NAME, 40, Vector2(320, 290), 1, 1);
        m_oName[i]->setColor(COLOR_BLACK);
        m_oName[i]->setCentered();
        m_oName[i]->setShadow(true, COLOR_WHITE);
        m_oName[i]->setRender(false);
        
        m_oCountBG[i] = new RenderableEntity("countBG", vPos + Vector2(40, 40));
        m_oCountBG[i]->setCentered();
        m_oCountBG[i]->setScale(Vector2(0.3f, 0.3f));

        m_oCount[i] = new TextEntity("0", FONT_NAME, 24, vPos + Vector2(40, 40), 1, 1);
        m_oCount[i]->setCentered();
        m_oCount[i]->setShadow();
        
        m_oPrice[i] = new TextEntity(getPrizeStr(data.iPrice), FONT_NAME, 24, vPos + Vector2(30, -50), 2, 1);
        m_oPrice[i]->setCenter(Vector2(1.0f, 0.5f));
        m_oPrice[i]->setShadow();

        m_oCoinIcon[i] = new RenderableEntity("icon_babycoin", vPos + Vector2(30, -50));
        m_oCoinIcon[i]->setCenter(Vector2(0.0f, 0.5f));
        m_oCoinIcon[i]->setScale(Vector2(0.3f, 0.3f));
        
        m_oEnter->addInEffect(m_oIcon[i]);
        m_oEnter->addInEffect(m_oName[i]);
        m_oEnter->addInEffect(m_oCountBG[i]);
        m_oEnter->addInEffect(m_oCount[i]);
        m_oEnter->addInEffect(m_oPrice[i]);
        m_oEnter->addInEffect(m_oCoinIcon[i]);
        
        m_oExit->addOutEffect(m_oIcon[i]);
        m_oExit->addOutEffect(m_oName[i]);
        m_oExit->addOutEffect(m_oCountBG[i]);
        m_oExit->addOutEffect(m_oCount[i]);
        m_oExit->addOutEffect(m_oPrice[i]);
        m_oExit->addOutEffect(m_oCoinIcon[i]);
    }
    
    m_oDescription = new TextEntity(" ", FONT_NAME, 28, Vector2(320, 260), 1, 0, true, 600);
    m_oDescription->setCenter(Vector2(0.5f, 1.0f));
    m_oDescription->setShadow();
    
    m_oSelect = new TextEntity("Select a PowerUp!", FONT_NAME, 48, Vector2(320, 300), 1, 0, true, 600);
    m_oSelect->setCenter(Vector2(0.5f, 1.0f));
    m_oSelect->setShadow();
    
    m_oSelectedMark = new RenderableEntity("halo", Vector2(0.0f, 0.0f));
    m_oSelectedMark->setCentered();
    m_oSelectedMark->setLiving(true);
    
    m_oBuyIcon = new RenderableEntity("MENU_0005_dialog2", Vector2(320, 100));
    m_oBuyIcon->setCentered();
    m_oBuyIcon->setScale(Vector2(0.7f, 0.7f));
    
    m_oBuyText = new TextEntity("Buy it!", FONT_NAME, 40, Vector2(320, 100), 1, 1);
    m_oBuyText->setCentered();
    m_oBuyText->setShadow();
    m_oBuyText->setColor(COLOR_PINK_TEXT);
    
    m_iSelected = -1;

    m_oEnter->addInEffect(m_oDescription);
    m_oEnter->addInEffect(m_oSelect);
    m_oEnter->addInEffect(m_oSelectedMark);
    m_oEnter->addInEffect(m_oBuyIcon);
    m_oEnter->addInEffect(m_oBuyText);

    m_oExit->addOutEffect(m_oDescription);
    m_oExit->addOutEffect(m_oSelect);
    m_oExit->addOutEffect(m_oSelectedMark);
    m_oExit->addOutEffect(m_oBuyIcon);
    m_oExit->addOutEffect(m_oBuyText);
}

void StateShopPowerUps::unloadContent()
{
    StateShopCommon::unloadContent();
}

void StateShopPowerUps::activated()
{    
    StateShopCommon::activated();
    
}

void StateShopPowerUps::enter()
{
    switch(m_ePopupState)
    {
        case SHOPPOPUP_NONE:
            m_iSelected = -1;
            break;
            
        case SHOPPOPUP_BUY:
            if(StatePopUp::instance().getResult())
            {
                //Bought the powerup!
                SaveGame sSaveGame = ProgressManager::instance().getSavedData();
                int iPrice = ProgressManager::instance().getPowerupData(m_iSelected).iPrice;
                sSaveGame.iCoins -= iPrice;
                sSaveGame.iCoinsSpent += iPrice;
                sSaveGame.iaPowerUps[ProgressManager::instance().getPowerupData(m_iSelected).eType] += ProgressManager::instance().getPowerupData(m_iSelected).iCount;
                ProgressManager::instance().setSavedData(sSaveGame);
            }
            break;
            
        default:
            break;
    }
    
    StateShopCommon::enter();
    
    for(int i=0; i<POWERUPS_ITEMS_COUNT; ++i)
    {
        m_oName[i]->setRender(i == m_iSelected);

        PowerupData data = ProgressManager::instance().getPowerupData(i);
        
        if(data.bShowCount)
        {
            m_oCount[i]->setText(getPrizeStr(ProgressManager::instance().getSavedData().iaPowerUps[data.eType]));
        }
        
        m_oIcon[i]->setColor(data.bEnabled ? COLOR_WHITE : COLOR_GREY);
    }
}

void StateShopPowerUps::buyClicked()
{
    if(!ProgressManager::instance().getPowerupData(m_iSelected).bEnabled)
        return;
    
    char buff[100];
    PowerupData data = ProgressManager::instance().getPowerupData(m_iSelected);
    if(ProgressManager::instance().getSavedData().iCoins >= data.iPrice)
    {
        sprintf(buff, "Buy %s for %s coins?", data.sCaption.c_str(), getPrizeStr(data.iPrice).c_str());
        m_ePopupState = SHOPPOPUP_BUY;
        StatePopUp::instance().popupConfirm(buff);
    }
    else 
    {
        StatePopUp::instance().popupConfirm("You don't have enough coins! Do you want to get some more coins?");
        m_ePopupState = SHOPPOPUP_NOCOINS;
    }
}

void StateShopPowerUps::update(float dt)
{
    StateShopCommon::update(dt);
    
    switch(m_eState)
    {
        case SHOP_IDLE:
        {
            if(m_oBack->isClicked())
            {
                m_oExit->start(true);
                m_eState = SHOP_EXIT;
                m_oNextState = &StateShop::instance();
            }
            else if(m_iSelected != -1 && m_oBuyIcon->isClicked())
            {
                //Buy popup
                buyClicked();
            }
            else
            {
                for(int i=0; i<POWERUPS_ITEMS_COUNT; ++i)
                {
                    if(m_oIcon[i]->isClicked())
                    {
                        if(i != m_iSelected)
                        {
                            if(m_iSelected != -1)
                                m_oName[m_iSelected]->setRender(false);
                            
                            m_iSelected = i;
                            m_oSelectedMark->setPosition(m_oIcon[i]->getPosition() + Vector2(0, 20));
                            
                            m_oName[m_iSelected]->setRender(true);
                            m_oDescription->setText(ProgressManager::instance().getPowerupData(i).sDescription);
                            
                            m_oBuyIcon->setColor(ProgressManager::instance().getPowerupData(m_iSelected).bEnabled ? COLOR_WHITE : COLOR_GREY);
                        }
                        else 
                        {
                            buyClicked();
                        }
                        break;
                    }
                }
            }
            
            break;
        }
        default:
            break;
    }
}

void StateShopPowerUps::render()
{
    StateShopCommon::render();
    
    for(int i=0; i<POWERUPS_ITEMS_COUNT; ++i)
    {
        m_oIcon[i]->render();
    }

    if(m_iSelected != -1)
    {
        m_oDescription->render();
        m_oSelectedMark->render();
        
        m_oBuyIcon->render();
        m_oBuyText->render();
    }
    else 
    {
        m_oSelect->render();
    }

    for(int i=0; i<POWERUPS_ITEMS_COUNT; ++i)
    {
        if(ProgressManager::instance().getPowerupData(i).bShowCount)
        {
            m_oCountBG[i]->render();
            m_oCount[i]->render();
        }
        
        m_oName[i]->render();
        
        m_oCoinIcon[i]->render();
        m_oPrice[i]->render();
    }
    
}

void StateShopPowerUps::exit()
{
    StateShopCommon::exit();
}

void StateShopPowerUps::setBackState()
{
    m_oNextState = &StateShop::instance();
}
